package org.ee.system.service;

import org.ee.core.api.PageResult;
import org.ee.system.domain.Role;
import org.ee.system.dto.RoleDto;
import org.ee.system.reository.RoleRepository;
import org.ee.system.vo.RoleVo;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 角色业务
 */
@Service
public class RoleService {
    private final RoleRepository roleRepository;


    public RoleService(RoleRepository roleRepository) {
        this.roleRepository = roleRepository;
    }


    public List<RoleVo> getRoles() {
        List<RoleVo> list=roleRepository.findAll().stream().map(role -> {
            RoleVo roleVo = new RoleVo();
            BeanUtils.copyProperties(role, roleVo);
            return roleVo;
        }).toList();

        return list;
    }
    /**
     *
     * @param id
     * @return
     */
    public Role findById(Long id) {
        return roleRepository.findById(id).orElse(null);
    }

    /**
     * 更新数据
     * @param roleDto
     * @return
     */
    public void saveOrUpdate(RoleDto roleDto) {
        if(roleDto.getId() != null) {
            Role role = roleRepository.findById(roleDto.getId()).orElse(null);
            if(roleDto.getName()!=null) {
                role.setName(roleDto.getName());
            }
            if(roleDto.getDescription()!=null) {
                role.setDescription(roleDto.getDescription());
            }
            if(roleDto.getCode()!=null) {
                role.setCode(roleDto.getCode());
            }
            if(roleDto.getSortNum()!=null) {
                role.setSortNum(roleDto.getSortNum());
            }
            if(roleDto.getStatus()!=null) {
                role.setStatus(roleDto.getStatus());
            }
            roleRepository.save(role);
        }else{
            Role role = new Role();
            BeanUtils.copyProperties(roleDto, role);
            roleRepository.save(role);
        }
    }

    /**
     * 删除
     * @param id
     */
    public void delete(Long id) {
        roleRepository.deleteById(id);
    }

    /**
     * 分页查询
     * @param page 当前页
     * @param size 每页记录数
     * @param name 名称
     * @return 分页数据
     */
    public PageResult<RoleVo> findByPage(int page, int size, String name) {
        PageRequest pageRequest = PageRequest.of(page - 1, size);
        Page<Role> pages;
        if (name == null && name.isEmpty()) {
            pages = roleRepository.findAll(pageRequest);
        } else {
            pages = roleRepository.findByNameContainingIgnoreCase(name, pageRequest);
        }
        PageResult<RoleVo> pageResult = new PageResult<>(
                pages.getTotalElements(),
                pages.getSize(),
                pages.getNumber() + 1,
                pages.getContent().stream().map(role -> {
                    RoleVo roleVo = new RoleVo();
                    BeanUtils.copyProperties(role, roleVo);
                    return roleVo;
                }).toList());
        return pageResult;
    }

}
